
// import CryptoJS from 'crypto-js';
//var baseURL = 'https://hlapi.wexiang.vip/';

	//正式
  var baseURL = '';
  var imgurl = '';
  
  
  
  if (process.env.NODE_ENV === 'development') {
	  // #ifdef H5
	  baseURL='bsapi'
	  imgurl = 'https://word.chaojidanci.com' + '/baozou-word/image/'
	  
	   uni.setStorageSync("token",'Bearer eyJhbGciOiJIUzI1NiJ9.eyJvcGVuaWQiOiJvUm5GZjE1VW9ZQnA2QlEwSUoxWXNLUmxXbFo4IiwiZXhwIjoxNzY1MDIxMjQwLCJ1c2VySWQiOjN9.qmOGxKPxWcVOytgXNmiZDxUM4LI_ntlUQ1DpIrk0Pb0')
	   uni.setStorageSync("user",{"accountNonExpired":false,"accountNonLocked":false,"authorities":[],"avatarUrl":"https://word.chaojidanci.com/baozou-word/image/imgkefu.png","credentialsNonExpired":false,"enabled":false,"id":3,"nickName":"00663","password":"","username":"00663"})
	 // #endif 
	 
	 // #ifdef MP-WEIXIN
		 baseURL='https://word.chaojidanci.com'
		 imgurl = baseURL + '/baozou-word/image/'
		//baseURL='https://bsapi'
		//baseURL='http://120.27.207.15:89'
	 // #endif
	 
  	console.log('开发环境');// 发布到生产环境时，此处代码会被摇树移除掉。
  } else {
	  baseURL='https://word.chaojidanci.com'
	  imgurl = baseURL + '/baozou-word/image/'
  	console.log('生产环境');
  }
  
  uni.setStorageSync('upimg', baseURL);
  
  function aesDecrypt(cipherTextBase64, keyBase64, fixedIv) {
      try {
          // 1. 解码密钥（Base64 -> WordArray，CryptoJS内部使用的二进制格式）
          const key = CryptoJS.enc.Base64.parse(keyBase64);
          
          // 2. 处理IV（UTF-8字符串 -> WordArray，确保与后端IV完全一致）
          const iv = CryptoJS.enc.Utf8.parse(fixedIv);
          
          // 3. 解码密文（Base64 -> WordArray）
          const cipherText = CryptoJS.enc.Base64.parse(cipherTextBase64);
          
          // 4. 配置解密参数（模式：CBC，填充：Pkcs7）
          const decryptConfig = {
              iv: iv,
              mode: CryptoJS.mode.CBC,
              padding: CryptoJS.pad.Pkcs7
          };
          
          // 5. 执行解密（返回WordArray）
          const decryptedWordArray = CryptoJS.AES.decrypt(
              { ciphertext: cipherText }, // 密文需包装为{ciphertext: ...}
              key,
              decryptConfig
          );
          
          // 6. 将解密结果转换为UTF-8字符串
          return CryptoJS.enc.Utf8.stringify(decryptedWordArray);
      } catch (error) {
          console.error("AES解密失败：", error);
          throw new Error("解密失败，请检查密钥、IV或密文是否正确");
      }
  }


export function request(app) {
	app.config.globalProperties.imgurl=imgurl
	app.config.globalProperties.http = (urls, data, method, baseurl,isload) => {
		uni.showLoading()
		var url
		if(baseurl){
			url = baseURL + urls
		}else{
			url = baseURL + urls
		}
		return new Promise(function(resolve, reject) {
			uni.request({
				url,
				header: {
					'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : ''
				},
				method: method ? method : 'GET',
				data: data ? data : {},
				success: (res) => {
					uni.hideLoading()
					if(res.statusCode == 200){
						
						// res.data.data = JSON.parse(aesDecrypt(res.data.data, '1jadAWPfm/BkY37vRjdk9w==', 'fixediv123456789'))
						
						if (res.data.code === 200||res.data.code==0) {
							resolve(res.data);
						}else{
							uni.showToast({
								duration: 2000,
								title: res.data.msg || res.data.message,
								icon: "none"
							})
							reject(res.data);
						}
					}else if (res.statusCode == 401) {
						uni.removeStorageSync('token')
						uni.removeStorageSync('user')
						uni.showToast({
							duration: 2000,
							title: '登录已过期，请重新登录',
							icon: "none"
						})
						uni.reLaunch({
							url:"/pages/user/index"
						})
						reject(res)
					} else {
						reject(res)
						if (res.message) {
							uni.showToast({
								duration: 2000,
								title: res.message,
								icon: "none"
							})
						} else {
							if(res.data.code !=11000){
								uni.showToast({
									duration: 2000,
									title: res.data.message,
									icon: "none"
								})
							}
						}
					}
				},
				complete() {
				    // uni.hideLoading()
				},
				fail: (res) => {
					reject(res)
					uni.showToast({
						duration: 2000,
						title: "系统频繁，稍后重试",
						icon: "none"
					})
				}
			})
		})
	}
	
	app.config.globalProperties.httpimg = (data,fileType,baseurl,method,isload) => {
		uni.showLoading()
		var url
		if(baseurl){
			url = baseURL + '/api/onDemand/getFileUrl'
		}else{
			url = baseURL + '/api/onDemand/getFileUrl'
		}
		return new Promise(function(resolve, reject) {
			uni.request({
				url,
				header: {
					'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : ''
				},
				method: method ? method : 'POST',
				data:{
					fileIds:data,
					fileType:fileType?fileType:1
				},
				success: (res) => {
					uni.hideLoading()
					if(res.statusCode == 200){
						
						// res.data.data = JSON.parse(aesDecrypt(res.data.data, '1jadAWPfm/BkY37vRjdk9w==', 'fixediv123456789'))
						
						if (res.data.code === 200||res.data.code==0) {
							resolve(res.data);
						}else{
							uni.showToast({
								duration: 2000,
								title: res.data.msg || res.data.message,
								icon: "none"
							})
							reject(res.data);
						}
					}else if (res.statusCode == 401) {
						uni.removeStorageSync('token')
						uni.removeStorageSync('user')
						uni.showToast({
							duration: 2000,
							title: '登录已过期，请重新登录',
							icon: "none"
						})
						uni.reLaunch({
							url:"/pages/user/index"
						})
						reject(res)
					} else {
						reject(res)
						if (res.message) {
							uni.showToast({
								duration: 2000,
								title: res.message,
								icon: "none"
							})
						} else {
							if(res.data.code !=11000){
								uni.showToast({
									duration: 2000,
									title: res.data.message,
									icon: "none"
								})
							}
						}
					}
				},
				complete() {
				    // uni.hideLoading()
				},
				fail: (res) => {
					reject(res)
					uni.showToast({
						duration: 2000,
						title: "系统频繁，稍后重试",
						icon: "none"
					})
				}
			})
		})
	}
}